package com.example.managersystem_01.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.managersystem_01.been.User;
import com.example.managersystem_01.been.UserMain;
import com.example.managersystem_01.service.impl.UserMainServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
public class TableController {

    @Autowired
    UserMainServiceImpl userMainService;

    @GetMapping("/userMain")
    public String mainTable(Model model) {

        List<UserMain> list = userMainService.list();

        model.addAttribute("userMain", list);
        return "/table/UserMain";
    }

    @GetMapping("/userDetail")
    public String userDetail(
            UserMain userMain,
            Model model) {
        model.addAttribute("checkUser", userMain);
        return "/table/UserDetail";
    }


    @GetMapping("/useModify/{name}")
    public String useModify(
            @PathVariable("name") String name,
            Model model
    ) {

        UpdateWrapper<UserMain> wrapper = new UpdateWrapper<>();
        wrapper.eq("name", name);

        List<UserMain> userMains = userMainService.list(wrapper);

        log.info("需要修改的用户为:{}", userMains);
        model.addAttribute("userModify", userMains);
        return "/table/UserModi";
    }

    @GetMapping("/userCenter")
    public String userCenter() {

        return "/table/UserMsg";
    }

    /**
     * 客户编写博客
     *
     * @return
     */
    @GetMapping("/userMsg")
    public String userMsg(
            HttpSession session,
            @RequestParam("text") String content,
            Model model
    ) {
        User user = (User) session.getAttribute("loginUser");
        UserMain userMain = new UserMain();

        log.info("需要写博客的用户为{}", user);

        userMain.setName(user.getName());
        userMain.setDate(new Date());
        userMain.setContent(content);

        userMainService.save(userMain);
        model.addAttribute("msg", "提交成功!");

        return "/table/UserMsg";
    }

    /**
     * 用户修改博客
     */
    @GetMapping("/userModifyTest/{name}")
    public String userModifyTest(
            @PathVariable("name") String name,
            @RequestParam("text") String text,
            Model model
    ) {
        UserMain userMain = new UserMain();
        userMain.setName(name);
        userMain.setContent(text);
        userMain.setDate(new Date());

        UpdateWrapper<UserMain> wrapper = new UpdateWrapper<UserMain>();
        wrapper.eq("name", name);

        userMainService.update(userMain, wrapper);
        return "/table/UserModi";
    }
}
